package com.interview.javabase.algorithm;

import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: zhangshuguang@vichain.com
 * @date: 2019/06/12 15:19
 */
@SpringBootTest
public class Demo7 {

    @Test
    public void solution1() {

        String s = "abpcplea";

        List<String> list = new ArrayList<>();
        list.add("ale");
        list.add("apple");
        list.add("monkey");
        list.add("plea");
        String longestWord = findLongestWord(s, list);
        System.out.println(longestWord);
    }

    private String findLongestWord(String s, List<String> d) {
        List max = new ArrayList();
        char[] chars = s.toCharArray();
        for (String s1 : d) {
            List<Character> list = new ArrayList<>();
            char[] chars1 = s1.toCharArray();
            for (int i1 = 0; i1 < chars1.length; i1++) {
                for (int i = i1; i < chars.length; i++) {
                    if (chars1[i1] == chars[i]) {
                        list.add(chars1[i1]);
                        break;
                    }
                }
            }

            if (max.size() < list.size()) {
                max = list;
            }
        }

        StringBuilder stringBuilder = new StringBuilder();
        max.forEach(s3 -> {
            stringBuilder.append(s3);
        });

        return stringBuilder.toString();
    }
}
